package com.xnyzc.lhy.resource.service.impl.worksheet;

import com.xnyzc.lhy.common.component.heli.HeLiUrlContant;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.DiDiException;
import com.xnyzc.lhy.common.penum.EDiDiErrorCode;
import com.xnyzc.lhy.common.util.RequestMethodUtil;
import com.xnyzc.lhy.resource.entity.worksheet.OaSysWorksheetEntity;
import com.xnyzc.lhy.resource.entity.worksheet.UpdateTicketStatusEntity;
import com.xnyzc.lhy.resource.entity.worksheet.UrgentTicketEntity;
import com.xnyzc.lhy.resource.entity.worksheet.param.heli.LoginSynchronousParam;
import com.xnyzc.lhy.resource.entity.worksheet.param.heli.RoleSynchronousParam;
import com.xnyzc.lhy.resource.service.worksheet.WorkSheetToHeLiService;
import com.xnyzc.lhy.resource.util.sign.CommonParam;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 调用合力工单 前端控制器
 * </p>
 *
 * @author cuichaoqun
 * @since 2019-11-14
 */
@Service
public class WorkSheetToHeLiServiceImpl implements WorkSheetToHeLiService {

    /**
     *
     *  调用合力
     *  创建工单接口
     *  第三方来源的工单推送到合力系统，需要传递渠道标识
     *
     * */
    @Override
    public Rv createTicket(OaSysWorksheetEntity oaSysWorksheetEntity) {
        String url = HeLiUrlContant.heLiUrl+ HeLiUrlContant.WORKFLOW_CREATE_TICKET;
        Rv rv = getRv(oaSysWorksheetEntity, url);
        return rv;
    }

    /**
     *  第三方系统创建的工单如果有加急操作需要实时同步到合力系统
     *
     * */
    @Override
    public Rv urgentTicket(UrgentTicketEntity urgentTicket) {
        String url = HeLiUrlContant.heLiUrl+ HeLiUrlContant.WORKFLOW_URGENT_TICKET;
        Rv rv = getRv(urgentTicket, url);
        return rv;
    }

    /**
     *  第三方系统推送过来的工单如果有状态改变操作需要实时同步到合力系统
     *
     * */
    @Override
    public Rv updateTicketStatus(UpdateTicketStatusEntity updateTicketStatusEntity) {
        String url = HeLiUrlContant.heLiUrl+ HeLiUrlContant.WORKFLOW_UPDATE_TICKETSTATUS;
        Rv rv = getRv(updateTicketStatusEntity, url);
        return rv;
    }

    /**
     * 登陆账号同步接口
     *
     * @return
     */
    @Override
    public Rv getLoginSynchronous(LoginSynchronousParam param) {
        String url = HeLiUrlContant.heLiUrl + HeLiUrlContant.LOGIN_SYNCHRONOUS_API;
        Rv rv = getRv(param, url);
        return rv;
    }

    /**
     * 客服角色同步接口
     *
     * @return
     */
    @Override
    public Rv getRoleSynchronous(RoleSynchronousParam param) {
        String url = HeLiUrlContant.heLiUrl + HeLiUrlContant.ROLE_SYNCHRONOUS_API;
        Rv rv = getRv(param, url);
        return rv;
    }


    private Rv getRv(Object obj, String url) {
        CommonParam commonParam = new CommonParam(obj);
        JSONObject requestParam = JSONObject.fromObject(commonParam);
        Rv rv;
        try {
            String data= RequestMethodUtil.requestMethodJson(requestParam,url);
            rv = com.alibaba.fastjson.JSONObject.parseObject(data, Rv.class);
        } catch (Exception e) {
            e.getMessage();
            throw DiDiException.create(EDiDiErrorCode.httpRequestHeLiError);
        }
        return rv;
    }
}
